import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.io.Serializable;


/**
 * Creates a JLabel timer for use with player turns
 * @author Sherman
 *
 */
public class TurnTimer extends JLabel implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;	//used for saving
	private boolean turnEnded;							//if the turn ended
	private int timeLeft;								//how much time is left
	private String timerText;							//text of how much time is left
	private boolean pubVisited;							//if the pub has been visited
	
	/**
	 * creates an instance of a turn timer for a player
	 */
	public TurnTimer(int timeLeft){
		this.timeLeft = timeLeft;
		turnEnded = false;
		timerText = "" + timeLeft;
		setText(timerText);	
		pubVisited = false;
	}
	
	public int getTimeRemaining() {
		return timeLeft;
	}

	/**
	 * starts the timer
	 */
	public void runTimer(){
		
		while ( timeLeft > 0){
				
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if (pubVisited){
				timeLeft = 1;
			}
			System.out.println(timeLeft);
			timeLeft -= 1;
			timerText = "" + timeLeft;
			setText(timerText);
		}
		
		turnEnded = true;
	}
	
	/**
	 * gets whether or not turn has ended
	 * @return if turn has ended
	 */
	public boolean turnEnded(){
		return turnEnded;
	}
	
	/**
	 * sets the turns to have ended or not
	 */
	public void setTurnEnded(boolean value){
		turnEnded = value;
	}
	
	/**
	 * gets the amount of time left on the timer
	 * @return time left on the timer
	 */
	public int getTimeLeft()
	{
		return timeLeft;
	}
	
	/**
	 * sets the pub to have been visited
	 * @param whether or not the pub was visited
	 */
	public void setPubVisited(boolean value){
		pubVisited = value;
	}
	
}